package com.yiwei.zxp.view;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

/**
 * Created by xsooy-pc on 18-5-11.
 */

public abstract class OnLoadMoreBottomListener extends RecyclerView.OnScrollListener {

    private int previousTotal = 0; // The total number of items in the dataset after the last load
    private boolean loading = true; // True if we are still waiting for the last set of home to load.
    private int visibleThreshold = 5; // The minimum amount of items to have below your current scroll position before loading more.


    private int currentPage = 1;

//    public boolean enable = true;


//    @Override
//    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//        super.onScrollStateChanged(recyclerView, newState);
//        if(!loading && !recyclerView.canScrollVertically(1)){
//            loading = true;
//            loadMoreDate();
//        }
//    }

    protected boolean isSlideToBottom(RecyclerView recyclerView) {
        if (recyclerView == null) return false;
        if (recyclerView.computeVerticalScrollExtent() + recyclerView.computeVerticalScrollOffset() >= recyclerView.computeVerticalScrollRange())
            return true;
        return false;
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
//        if (!enable) {
//            return;
//        }

        LinearLayoutManager llm = (LinearLayoutManager) recyclerView.getLayoutManager();
//        findFirstVisibleItemPosition()
//        findFirstCompletelyVisibleItemPosition()
//        findLastVisibleItemPosition()
//        findLastCompletelyVisibleItemPosition()

//          //当前屏幕所看到的子项个数
        int visibleItemCount = recyclerView.getChildCount();
        //当前RecyclerView的所有子项个数
        int totalItemCount = llm.getItemCount();
        //屏幕中第一个可见子项的position
        int firstVisibleItem = llm.findFirstVisibleItemPosition();
        if (loading) {
            if (totalItemCount > previousTotal) {
                loading = false;
                previousTotal = totalItemCount;
            }
        }
        if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
//        if (!loading && (visibleItemCount + firstVisibleItem) >= totalItemCount){
            currentPage++;

            onLoadMore(currentPage);

            loading = true;
        }

//        int topRowVerticalPosition = (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();
//        swipeRefreshLayout.setEnabled(topRowVerticalPosition >= 0);
        if (isSlideToBottom(recyclerView)) {
            onBottom();
        }
    }

    public void setPage(int page) {
        currentPage = page;
        this.previousTotal = 0;
        this.loading = true;
//        enable = true;
    }

    public abstract void onLoadMore(int page);

    public abstract void onBottom();
}
